export default class FormEngineContext {
    ctx;
    vue;
    formDefinition;
    view;
    formModel;

    constructor(ctx) {
        this.ctx = ctx;
        this.init()
    }

    init() {
        this.vue = this.ctx.vue;
        this.formDefinition = this.ctx.formDefinition;
        this.formModel = this.ctx.formModel;
        this.view = this.ctx.view;

        this.buildFormModelIfNecessary()
    }

    buildFormModelIfNecessary() {
        this.formModel = (this.formModel || this.buildFormModel());
        this.vue.change(this.formModel)
    }

    buildFormModel() {
        let res = {};
        (this.formDefinition && this.formDefinition.fields || []).forEach(f => {
            this.vue.$set(res, f.fieldName, undefined);
            f.fieldTitleName && this.vue.$set(res, f.fieldTitleName, undefined);
        })
        return res;
    }
};